package com.zbl.zblproject.core.controller;

import com.alibaba.fastjson.JSONArray;
import com.zbl.zblproject.core.entity.OrderItem;
import com.zbl.zblproject.core.entity.Result;
import com.zbl.zblproject.core.mapper.OrderItemMapper;
import com.zbl.zblproject.core.service.OrderItemService;
import com.zbl.zblproject.core.utils.ResultUtil;
import com.zbl.zblproject.erp.entity.ErpTempOrderItem;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.ibatis.annotations.Param;
import org.apache.poi.hssf.usermodel.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.List;

/**
 * @author youlanqiang
 * @version 1.0
 * @date 2018/10/30
 */
@RestController
@RequestMapping("/order-item")
@Api(tags = "商品订单项接口")
public class OrderItemController  {

    @Autowired
    private OrderItemService orderItemService;

    @Autowired
    private OrderItemMapper orderItemMapper;


    @DeleteMapping("/delete/{id}")
    @ApiOperation("删除商品订单项")
    public Result deleteItem(@ApiParam("订单项ID") @PathVariable int id){
        try{
            orderItemService.deleteItemById(id);
            return ResultUtil.success("成功!");
        }catch(RuntimeException e){
            return ResultUtil.error("删除失败!");
        }

    }

    @PostMapping("/update/{id}")
    @ApiOperation("更新商品订单项")
    public Result updateItemNumber(@ApiParam("订单项ID") @PathVariable int id,
                                   @ApiParam("修改数量") @RequestParam("num") int number){
            try{

            }catch(RuntimeException e){

            }
            return ResultUtil.error("该接口已经弃用,请使用更新所有订单项目接口.");
    }


    @PostMapping("/update")
    @ApiOperation("更新商品的所有订单项目")
    public Result updateItemNumbers(@ApiParam("订单项") @RequestParam("items") String items){
            try{
                List<ErpTempOrderItem> list = JSONArray.parseArray(items, ErpTempOrderItem.class);
                orderItemService.uploadNumberOfItems(list);
                return ResultUtil.success("更新成功!");
            }catch(NullPointerException e){
                return ResultUtil.error("更新失败!");
            }
    }

    @PostMapping("/listOrderItem")
    @ApiOperation("查询所有未入库订单信息")
    @ResponseBody
    public Result selectAllOrderItem(){
        List<OrderItem> orderItemList = orderItemMapper.selectAllOrderItem();
        return ResultUtil.success("查询所有未入库订单信息成功",orderItemList);
    }


    @PostMapping("/deleteOrderItem/{id}")
    @ApiOperation("根据id删除未入库订单信息")
    @ResponseBody
    public Result deleteOrderItem(@ApiParam("未入库订单ID")@PathVariable String id){
        return orderItemService.deleteOrderItem(id);
    }

    @PostMapping("/updateOrderItem")
    @ApiOperation("根据id修改未入库订单数量")
    @ResponseBody
    public Result updateOrderItem(OrderItem orderItem){
        return orderItemService.updateOrderItem(orderItem);
    }

    @GetMapping(value="/exportOrderItem/{id}",produces = {"application/vnd.ms-excel;charset=UTF-8"})
    @ApiOperation("导出未入库订单信息")
    public String exportOrderItem(HttpServletResponse response,@PathVariable("id") String id) throws IOException {
        return orderItemService.exportOrderItem(response,id);
    }


}
